package com.ruoyi.gczl.sybg.dq.lxk.controller;

import com.ruoyi.common.GetFileName;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gczl.sybg.dq.lxk.domain.SybgLxkdq;
import com.ruoyi.gczl.sybg.dq.lxk.service.ISybgLxkdqService;
import com.ruoyi.infoRunTaskData.domain.InfoRunTaskData;
import com.ruoyi.infoRunTaskData.service.IInfoRunTaskDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 试验报告龙溪口电气Controller
 *
 * @author huacai
 * @date 2024-08-22
 */
@RestController
@RequestMapping("/sybg/lxkdq")
public class SybgLxkdqController extends BaseController {
    @Resource
    private ISybgLxkdqService sybgLxkdqService;

    @Resource
    private IInfoRunTaskDataService iInfoRunTaskDataService;

    /**
     * 查询试验报告龙溪口电气列表
     */
    @PreAuthorize("@ss.hasPermi('sybg:lxkdq:list')")
    @GetMapping("/list")
    public TableDataInfo list(SybgLxkdq sybgLxkdq) {
        startPage();
        List<SybgLxkdq> list = sybgLxkdqService.selectSybgLxkdqList(sybgLxkdq);
        return getDataTable(list);
    }

    /**
     * 导出试验报告龙溪口电气列表
     */
    @PreAuthorize("@ss.hasPermi('sybg:lxkdq:export')")
    @Log(title = "试验报告龙溪口电气", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SybgLxkdq sybgLxkdq) {
        List<SybgLxkdq> list = sybgLxkdqService.selectSybgLxkdqList(sybgLxkdq);
        ExcelUtil<SybgLxkdq> util = new ExcelUtil<SybgLxkdq>(SybgLxkdq.class);
        util.exportExcel(response, list, "试验报告龙溪口电气数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SybgLxkdq> util = new ExcelUtil<SybgLxkdq>(SybgLxkdq.class);
        util.importTemplateExcel(response, "试验报告龙溪口电气数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "试验报告龙溪口电气", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('sybg:lxkdq:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<SybgLxkdq> util = new ExcelUtil<SybgLxkdq>(SybgLxkdq.class);
        InputStream inputStream = file.getInputStream();
        List<SybgLxkdq> list = util.importExcel(inputStream);
        inputStream.close();
        int count = sybgLxkdqService.batchInsertSybgLxkdq(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取试验报告龙溪口电气详细信息
     */
    @PreAuthorize("@ss.hasPermi('sybg:lxkdq:query')")
    @GetMapping(value = "/{sybgLxkdqId}")
    public AjaxResult getInfo(@PathVariable("sybgLxkdqId") Integer sybgLxkdqId) {
        return success(sybgLxkdqService.selectSybgLxkdqBySybgLxkdqId(sybgLxkdqId));
    }

    /**
     * 新增试验报告龙溪口电气
     */
    @PreAuthorize("@ss.hasPermi('sybg:lxkdq:add')")
    @Log(title = "试验报告龙溪口电气", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SybgLxkdq sybgLxkdq) {
        sybgLxkdq.setUserId(Math.toIntExact(getUserId()));
        sybgLxkdq.setDeptId(Math.toIntExact(getDeptId()));
        sybgLxkdq.setCreateBy(getUsername());
        int i = sybgLxkdqService.insertSybgLxkdq(sybgLxkdq);
        //向流程业务表中加入数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessKey(String.valueOf(sybgLxkdq.getSybgLxkdqId()));
        infoRunTaskData.setBusinessType(3); //业务模块: 试验报告-电气-龙溪口
        String fileNameOnly = GetFileName.getFileNameOnly(sybgLxkdq.getFileName());
        infoRunTaskData.setBusinessTitle(fileNameOnly); //任务名称: 文件名称
        infoRunTaskData.setApplyUser(SecurityUtils.getLoginUser().getUser().getNickName());//申请人

        iInfoRunTaskDataService.insertInfoRunTaskData(infoRunTaskData);

        return toAjax(i);
    }

    /**
     * 修改试验报告龙溪口电气
     */
    @PreAuthorize("@ss.hasPermi('sybg:lxkdq:edit')")
    @Log(title = "试验报告龙溪口电气", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SybgLxkdq sybgLxkdq) {
        //修改业务中间表关系数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        String fileNameOnly = GetFileName.getFileNameOnly(sybgLxkdq.getFileName());
        infoRunTaskData.setBusinessTitle(fileNameOnly); //任务名称: 文件名称
        infoRunTaskData.setBusinessKey(String.valueOf(sybgLxkdq.getSybgLxkdqId()));
        infoRunTaskData.setBusinessType(3); //业务模块: 试验报告-电气-龙溪口
        iInfoRunTaskDataService.updateInfoRunTaskDatas(infoRunTaskData); //注意updateInfoRunTaskDatas

        return toAjax(sybgLxkdqService.updateSybgLxkdq(sybgLxkdq));
    }

    /**
     * 删除试验报告龙溪口电气
     */
    @PreAuthorize("@ss.hasPermi('sybg:lxkdq:remove')")
    @Log(title = "试验报告龙溪口电气", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sybgLxkdqIds}")
    public AjaxResult remove(@PathVariable Integer[] sybgLxkdqIds) {
        //同时删除业务关系数据
        for (Integer sybgLxkdqId : sybgLxkdqIds) {
            SybgLxkdq sybgLxkdq = sybgLxkdqService.selectSybgLxkdqBySybgLxkdqId(sybgLxkdqId);
            iInfoRunTaskDataService.deleteData(String.valueOf(sybgLxkdq.getSybgLxkdqId()), 2);//业务模块: 试验报告-电气-龙溪口
        }
        return toAjax(sybgLxkdqService.deleteSybgLxkdqBySybgLxkdqIds(sybgLxkdqIds));
    }
}
